Refactor lexer state management to use save/restore pattern#245
Merged
Conversation
…restore - Move ANplusBParser from per-call allocation in parse_nth_expression() to a field on SelectorParser, initialized once in the constructor. This avoids allocating a new Lexer for every :nth-child()/:nth-of-type() etc. encountered. - Replace the temporary `new Lexer()` in SelectorParser.parse_lang_identifiers() and AtRulePreludeParser.parse_feature_value() with lexer.save_position() / restore_position(). The saved LexerPosition object is a plain struct with no heap cost beyond the object itself, and avoids re-allocating a full Lexer + re-initializing its source reference for each :lang() argument range and each media-feature value range. https://claude.ai/code/session_01CQeKNnXidD5EQVJY4xBMMp
Bundle ReportChanges will increase total bundle size by 18 bytes (0.01%) ⬆️. This is within the configured threshold ✅ Detailed changes
Affected Assets, Files, and Routes:view changes for bundle: @projectwallace/css-parser-esmAssets Changed:
Files in
Files in
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #245 +/- ##
==========================================
- Coverage 93.15% 93.14% -0.01%
==========================================
Files 17 17
Lines 3038 3035 -3
Branches 845 845
==========================================
- Hits 2830 2827 -3
Misses 208 208 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Eliminates per-call
Lexerallocations in the hot paths ofSelectorParserandAtRulePreludeParser:ANplusBParserinparse_nth_expression()— was allocating a newANplusBParser(and therefore a newLexer) for every:nth-child(),:nth-of-type(), etc. encountered. Promoted to a field onSelectorParser, initialized once in the constructor.Lexerinparse_lang_identifiers()— replacednew Lexer()+ reference swap withlexer.save_position()/restore_position().Lexerinparse_feature_value()— same save/restore replacement.Benchmark results
Tested against
main(Bootstrap 273 KB, Tailwind 3.5 MB). ops/sec, higher is better.mainTokenizer-only benchmarks are unchanged (expected — these changes only affect the selector and prelude parsers).
https://claude.ai/code/session_01CQeKNnXidD5EQVJY4xBMMp